﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SysMisc;

namespace Kernel
{
    public partial class CategoryViewer : UserControlEx,IControlEx
    {
        public CategoryViewer()
        {
            InitializeComponent();

            UI_Init();
        }

        public Project Project { set { _prj = value; } }

        private Project _prj;


        public void UI_Init()
        {
            this.Dock = DockStyle.Fill;

            string[] v_names = { "PaperCode", "PaperName", "PaperType", "PaperSpec", "FileName", "FileType" };
            string[] v_headerTexts = { "图纸编号", "图纸名称", "图纸类型","图纸规格", "文件名","文件类型" };
            int[] v_widths = { 120, 120, 80, 140, 320, 80, 90, 90, 220, 140, 140, 90 };
            bool[] v_visables = { true, true, true, true, true, true, true, true, true, true, true, false };
            bool[] v_readOnlys = { false, false, false, false, false, false, false, false, false, false, false, true };
            string[] v_types = { "tb", "tb", "tb", "tb", "tb", "tb", "tb", "cb", "tb", "tb", "tb", "cb" };

            int n = v_names.Length;
            for (int i = 0; i < n; i++)
            {
                DataGridViewColumn dgvc = new DataGridViewColumn();
                dgvc.DataPropertyName = v_names[i];
                dgvc.HeaderText = v_headerTexts[i];
                dgvc.Width = v_widths[i];
                dgvc.Visible = v_visables[i];
                dgvc.ReadOnly = v_readOnlys[i];
                switch (v_types[i])
                {
                    case "tb":
                        dgvc.CellTemplate = new DataGridViewTextBoxCell();
                        break;
                    case "cb":
                        dgvc.CellTemplate = new DataGridViewCheckBoxCell();
                        break;
                }
                this.dataGridView_main.Columns.Add(dgvc);
            }

            this.dataGridView_main.AutoGenerateColumns = false;
            this.dataGridView_main.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView_main.MultiSelect = false;
        }

        public void UI_Load()
        {

        }

        public void UI_Update()
        {
            this.dataGridView_main.DataSource = new BindingList<xmlCatalogue>(_prj.ProjectConfig.Catalogues);
        }

        public void Save()
        {
            _prj.ProjectConfig.Catalogues = new List<xmlCatalogue>((BindingList<xmlCatalogue>)this.dataGridView_main.DataSource);
        }

        private void Button_Paper_Click(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            switch (btn.Text)
            {
                case "1.更新图纸名称":
                    {
                        if(true == _prj.OpenAllFilesOfPrj())
                        {
                            BindingList<xmlCatalogue> catgs_cus = new BindingList<xmlCatalogue>();
                            //从目录条目中读取所有的自定义条目
                            BindingList<xmlCatalogue> catgs = this.dataGridView_main.DataSource as BindingList<xmlCatalogue>;
                            foreach (xmlCatalogue catg in catgs)
                                if (true == catg.IsCustomer)
                                    catgs_cus.Add(catg);
                            //从当前文档中读取所有图纸条目
                            BindingList<xmlCatalogue> catgs_new = new BindingList<xmlCatalogue>( _prj.GetAllPaperNames());
                            //当自定义条目，重新添加到图纸目录中
                            foreach (xmlCatalogue catg in catgs_cus)
                                catgs_new.Add(catg);
                            this.dataGridView_main.DataSource = catgs_new;
                        }
                        _prj.IsUpdated = true;
                    }
                    break;
                case "3.生成图纸编号":
                    {
                        _prj.ReCreateAllPaperCode();
                        /*重新更新数据*/
                        this.dataGridView_main.DataSource = new BindingList<xmlCatalogue>(_prj.ProjectConfig.Catalogues);
                        _prj.IsUpdated = true;
                    }
                    break;
                case "4.写入编号到图纸":
                    {
                        _prj.UpdateAllPapers();
                        _prj.FillCategoryPaperItems();

                        Misc.Alert("更新完成！");
                        _prj.IsUpdated = true;
                    }
                    break;
                case "2.导入排序模板":
                    {
                        OpenFileDialog ofd = new OpenFileDialog();
#if CAD_2008
                        ofd.FileName = @"";
#elif CAD_2014
                        ofd.FileName = @"Template\";
#endif
                        ofd.Filter = "Excel文件(*.xls) | *.xls";//文件类型
                        DialogResult dr = ofd.ShowDialog();
                        if (dr == DialogResult.OK)
                        {
                            ExcelHelper eh = new ExcelHelper(ofd.FileName);
                            DataTable dt = eh.ExcelToDataTable("Main",true);
                            _prj.DicOrderTemplateItem = OrderTempItem.FillDictionaryByDataTable(dt);

                            _prj.ReOrderAllPapers();
                            
                            /*重新更新数据*/
                            this.dataGridView_main.DataSource = new BindingList<xmlCatalogue>(_prj.ProjectConfig.Catalogues);


                            Misc.Alert("重新排序完成！");
                            _prj.IsUpdated = true;
                        }
                    }
                    break;
            }
        }

        private void Button_PaperItem_Click(object sender, EventArgs e)
        {
            BindingList<xmlCatalogue> lst_ctg = (BindingList<xmlCatalogue>)dataGridView_main.DataSource;
            int index = 0;
            if (dataGridView_main.SelectedRows.Count > 0)
                index = dataGridView_main.SelectedRows[0].Index;

            Button btn = (Button)sender;
            switch (btn.Text)
            {
                case "上移":
                    {
                        if(dataGridView_main.CurrentRow.Index <= 0)
                        {
                            return;
                        }
                        else
                        {
                            xmlCatalogue tempRow = lst_ctg[index];
                            lst_ctg.RemoveAt(index);
                            lst_ctg.Insert(index - 1,tempRow);
                            dataGridView_main.Rows[index - 1].Selected = true;
                        }
                    }
                    break;
                case "下移":
                    {
                        if(index == dataGridView_main.Rows.Count-1)
                        {
                            return;
                        }
                        else if(index == -1)
                        {
                            return;
                        }
                        else
                        {
                            xmlCatalogue tempRow = lst_ctg[index+1];
                            lst_ctg.RemoveAt(index+1);
                            lst_ctg.Insert(index, tempRow);
                            dataGridView_main.Rows[index + 1].Selected = true;
                        }
                    }
                    break;
                case "置顶":
                    {
                        xmlCatalogue tempRow = lst_ctg[index];
                        lst_ctg.RemoveAt(index);
                        lst_ctg.Insert(0, tempRow);
                        dataGridView_main.Rows[0].Selected = true;
                    }
                    break;
                case "置底":
                    {
                        xmlCatalogue tempRow = lst_ctg[index];
                        lst_ctg.RemoveAt(index);
                        lst_ctg.Add( tempRow);
                        dataGridView_main.Rows[lst_ctg.Count-1].Selected = true;
                    }
                    break;
                case "添加":
                    {
                        using (FormCatalogue frmCat = new FormCatalogue())
                        {
                            frmCat.ShowInTaskbar = false;
                            DialogResult dr = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frmCat);
                            if( dr == DialogResult.OK)
                            {
                                lst_ctg.Add(frmCat.CatalogueItem);
                            }
                        }
                    }
                    break;
                case "删除":
                    {
                        lst_ctg.RemoveAt(index);
                    }
                    break;
            }
        }

#region 实现IControlEx接口
        void IControlEx.Init() { }
        void IControlEx.Load() { UI_Load(); }
        void IControlEx.Update() { UI_Update(); }
        void IControlEx.Save() { Save(); }
        bool IControlEx.Updated { get; }
#endregion
    }
}
